﻿#include "stdafx.h"
#include "XButtonIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "Util.h"
#include "IXColorDraw.h"
#include "IXImageDraw.h"
#include "IXButton.h"

#include "ParseHelper.h"


CXButtonIntParser::CXButtonIntParser() : CXIntParserBase(&TIntCreateControl<IXButton>)
{
    ;
}

CXButtonIntParser::~CXButtonIntParser()
{
    ;
}

// 填充属性
BOOL CXButtonIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;
    XString strTemp;
    LONG lTemp = 0;
    COLORREF clrTemp;
    HRESULT hResult;

    IXButton* pButton = Util::QueryIntf<IXButton>(pControl);
    if(pButton == NULL)
        return FALSE;

    // XNC
    XBSTR bstrXNC;
    hResult = pNode->GetValue(_T("xnc"), &bstrXNC);
    if(SUCCEEDED(hResult))
    {
        LONG nHorzCount=4, nVertCount=1;
        hResult = pNode->GetValue(_T("hcount"), &nHorzCount);
        nHorzCount = SUCCEEDED(hResult) ? nHorzCount : 4;
        hResult = pNode->GetValue(_T("vcount"), &nVertCount);
        nVertCount = SUCCEEDED(hResult) ? nVertCount : 1;

        ImageDrawParams Param = {0};
        hResult = pNode->GetValue(_T("params"), &bstrTemp);

        ImageDrawParams* pParam = NULL;
        if(SUCCEEDED(hResult))
        {
            if(_tcsicmp((LPCTSTR)(BSTR)bstrTemp, _T("null")) != 0
                && ParseHelper::ParseImageDrawParams(bstrTemp, Param))
                pParam = &Param;
        }
        pButton->put_ImagePath((LPCTSTR)(BSTR)bstrXNC, nHorzCount, nVertCount, pParam);
    }

    // AlphaMode
    hResult = pNode->GetValue(_T("alpha"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        struct stModeInfo
        {
            LPCTSTR          szModeName;
            eImageAlphaMode  mode;
        } data[] =
        {
            {_T("bitblt"),      Alpha_BitBlt},
            {_T("defAlpha"),    Alpha_DefinedAlpha},
            {_T("AlphaVal"),    Alpha_AlphaValue},
            {_T("TransColor"),  Alpha_TransColor},
            {_T("TransAlpha"),  Alpha_TransAlpha},
        };
        for(int i=0; i<_countof(data); ++ i)
        {
            if(_tcsicmp(data[i].szModeName, (LPCTSTR)(BSTR)bstrTemp) == 0)
            {
                pButton->put_AlphaMode(data[i].mode);
                break;
            }
        }
    }

    // AlphaValue
    hResult = pNode->GetValue(_T("alphavalue"), &lTemp);
    if(SUCCEEDED(hResult))
        pButton->put_AlphaValue(lTemp & 0xFF);

    // TransparentColor
    hResult = pNode->GetValue(_T("transClr"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        if(ParseHelper::ParseColor(bstrTemp, clrTemp))  // TODO: pNode添加一个对Color的解析
            pButton->put_TransparentColor(clrTemp);
    }

    return bResult;
}
